package com.animerank.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.animerank.service.IVoteService;
import com.animerank.dto.VoteDTO;
import com.animerank.dto.Result;
import com.animerank.entity.Vote;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 投票控制器
 */
@RestController
@RequestMapping("/vote")
@Tag(name = "投票管理", description = "投票、票数统计等接口")
public class VoteController {

    @Autowired
    private IVoteService voteService;

    /**
     * 投票接口
     */
    @PostMapping
    @SaCheckLogin
    @Operation(summary = "投票", description = "为动漫投票，需要登录")
    public Result<?> vote(@Valid @RequestBody VoteDTO voteDTO, HttpServletRequest request) {
        // 获取用户IP地址
        String ip = request.getRemoteAddr();
        return voteService.vote(voteDTO, ip);
    }

    /**
     * 查询动漫总票数
     */
    @GetMapping("/count/{animeId}")
    @Operation(summary = "查询动漫票数", description = "查询指定动漫的总票数")
    public Result<Integer> getAnimeVoteCount(@PathVariable Long animeId) {
        return voteService.getAnimeVoteCount(animeId);
    }
    
    /**
     * 获取用户投票历史（支持分页）
     */
    @GetMapping("/user/list")
    @SaCheckLogin
    @Operation(summary = "获取用户投票历史", description = "获取当前登录用户的投票历史记录")
    public Result<Map<String, Object>> getUserVoteHistory(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "9") Integer pageSize) {
        return voteService.getUserVoteHistory(page, pageSize);
    }

}